﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using JumbotCms.Common;

namespace JumbotCms.WebFile.MTag
{
    public partial class _view_threadadd : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User_Load("", "html");
            string mtagid = Str2Str(q("mtagid"));
            if (q("act") != "save")
            {
                string PageStr = "";
                if (mtagid != "0")
                {
                    PageStr = GetPageHtml("mtag_threadadd1");
                    //是不是发言人
                    if (IsMTagTalker(UserId, mtagid))
                    {
                        doh.Reset();
                        doh.SqlCmd = "SELECT a.id as mtagid,a.Title as mtagname FROM " +
                            " [jcms_normal_mtag] as a left join [jcms_normal_mtag_sort] as b" +
                            " on b.id=a.SortId " +
                            " WHERE a.id=" + mtagid;
                        DataTable dtMTag = doh.GetDataTable();
                        for (int i = 0; i < dtMTag.Columns.Count; i++)
                        {
                            PageStr = PageStr.Replace("{$_" + dtMTag.Columns[i].ColumnName.ToLower() + "$}", dtMTag.Rows[0][i].ToString());
                        }
                        dtMTag.Clear();
                    }
                    else
                    {
                        Response.Write("权限不足");
                        Response.End();
                    }
                }
                else
                {
                    PageStr = GetPageHtml("mtag_threadadd0");
                }
                Response.Write(PageStr);
            }
            else
            {
                if (!CheckFormUrl())
                    Response.End();
                string _code = f("txtCode");
                if (Jumbot.Common.Cookie.GetValue("ValidateCode") != _code)
                {
                    Response.Write("JumbotCms.Alert('验证码错误', '0');");
                    Response.End();
                }
                doh.Reset();
                doh.AddFieldItem("Title", f("txtTitle"));
                doh.AddFieldItem("Content", GetCutString(Jumbot.Common.Strings.HtmlEncode(f("txtContent")), 2000));
                doh.AddFieldItem("MTagId", Str2Str(f("txtMTagId")));
                doh.AddFieldItem("IsGood", 0);
                doh.AddFieldItem("IsHead", 0);
                doh.AddFieldItem("ReplyCount", 0);
                doh.AddFieldItem("AddTime", DateTime.Now.ToString());
                doh.AddFieldItem("UserId", UserId);
                int _newid = doh.Insert("jcms_normal_mtag_thread");
                if (_newid > 0)
                {
                    doh.Reset();
                    doh.ConditionExpress = "id=" + Str2Str(f("txtMTagId"));
                    doh.AddFieldItem("UpdateTime", DateTime.Now.ToString());
                    doh.Update("jcms_normal_mtag");
                    Response.Write("JumbotCms.Alert('发表成功', '1', \"window.location='" + site.Dir + "mtag/view_threadview/index.aspx?threadid=" + _newid + "&mtagid=" + Str2Str(f("txtMTagId")) + "';\");");
                }
                else
                    Response.Write("JumbotCms.Alert('提交有误', '0');");
            }
        }
    }
}
